using System;
using EPiServer.Common;
using EPiServer.Common.Security;
using EPiServer.Community;

namespace EPiAbstractions.Community
{
    public class DefaultSecurityHandlerFacade : IDefaultSecurityHandlerFacade
    {
        private static DefaultSecurityHandlerFacade _instance;

        public DefaultSecurityHandlerFacade()
        {
            CommunitySystem.CurrentContext.DefaultSecurity.RemovedGroup += OnRemovedGroup;
            CommunitySystem.CurrentContext.DefaultSecurity.CreatedGroup += OnCreatedGroup;
            CommunitySystem.CurrentContext.DefaultSecurity.UpdatedGroup += OnUpdatedGroup;
            CommunitySystem.CurrentContext.DefaultSecurity.CreatedUser += OnCreatedUser;
            CommunitySystem.CurrentContext.DefaultSecurity.RemovedUser += OnRemovedUser;
            CommunitySystem.CurrentContext.DefaultSecurity.RestoredUser += OnRestoredUser;
            CommunitySystem.CurrentContext.DefaultSecurity.UpdatedUser += OnUpdatedUser;
            CommunitySystem.CurrentContext.DefaultSecurity.ChangedUserPassword += OnChangedUserPassword;
        }

        public static DefaultSecurityHandlerFacade Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new DefaultSecurityHandlerFacade();

                return _instance;
            }

            set { _instance = value; }
        }

        #region IDefaultSecurityHandlerFacade Members

        public event GroupRemoveEventHandler RemovedGroup;
        public event GroupCreateEventHandler CreatedGroup;
        public event GroupUpdateEventHandler UpdatedGroup;
        public event UserCreateEventHandler CreatedUser;
        public event RemoveUserEventHandler RemovedUser;
        public event RestoreUserEventHandler RestoredUser;
        public event UpdateUserEventHandler UpdatedUser;
        public event ChangeUserPasswordEventHandler ChangedUserPassword;

        public virtual Boolean AuthenticateUser(String userName, String passWord, out IUser user)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.AuthenticateUser(userName, passWord, out user);
        }

        public virtual IGroup AddGroup(IGroup group)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.AddGroup(group);
        }

        public virtual IUser AddUser(IUser user)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.AddUser(user);
        }

        public virtual IGroup GetGroup(Int32 groupId)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetGroup(groupId);
        }

        public virtual IGroup GetGroupByName(String name)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetGroupByName(name);
        }

        public virtual IUser GetUser(Int32 userId)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetUser(userId);
        }

        public virtual IUser GetUserByUserName(String userName)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetUserByUserName(userName);
        }

        public virtual UserCollection GetUsersByEMail(String email)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetUsersByEMail(email);
        }

        public virtual UserCollection GetUsersByAlias(String alias)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetUsersByAlias(alias);
        }

        public virtual IUser GetUser(Int32 userId, Boolean loadAll)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetUser(userId, loadAll);
        }

        public virtual GroupCollection GetGroups()
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetGroups();
        }

        public virtual UserCollection GetUsers()
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetUsers();
        }

        public virtual UserCollection GetGroupUsers(IGroup group)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetGroupUsers(group);
        }

        public virtual GroupCollection GetUserGroups(IUser user)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetUserGroups(user);
        }

        public virtual GroupCollection GetParentGroups(IGroup childGroup)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetParentGroups(childGroup);
        }

        public virtual GroupCollection GetChildGroups(IGroup parentGroup)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetChildGroups(parentGroup);
        }

        public virtual void RemoveGroup(IGroup group)
        {
            CommunitySystem.CurrentContext.DefaultSecurity.RemoveGroup(group);
        }

        public virtual void RemoveUser(IUser user)
        {
            CommunitySystem.CurrentContext.DefaultSecurity.RemoveUser(user);
        }

        public virtual void RemoveUser(IUser user, Boolean permanent)
        {
            CommunitySystem.CurrentContext.DefaultSecurity.RemoveUser(user, permanent);
        }

        public virtual void UpdateGroup(IGroup group)
        {
            CommunitySystem.CurrentContext.DefaultSecurity.UpdateGroup(group);
        }

        public virtual void UpdateUser(IUser user)
        {
            CommunitySystem.CurrentContext.DefaultSecurity.UpdateUser(user);
        }

        public virtual GroupCollection GetModuleGroups(String module)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetModuleGroups(module);
        }

        public virtual UserCollection GetModuleUsers(String module)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetModuleUsers(module);
        }

        public virtual GroupCollection GetSiteGroups(ISite site)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetSiteGroups(site);
        }

        public virtual UserCollection GetSiteUsers(ISite site)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetSiteUsers(site);
        }

        public virtual GroupCollection FindGroups(String searchFor)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.FindGroups(searchFor);
        }

        public virtual UserCollection FindUsers(String searchFor)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.FindUsers(searchFor);
        }

        public virtual GroupCollection GetAdministrativeAccessRightGroups(Int32 sectionId)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetAdministrativeAccessRightGroups(sectionId);
        }

        public virtual UserCollection GetAdministrativeAccessRightUsers(Int32 sectionId)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetAdministrativeAccessRightUsers(sectionId);
        }

        public virtual void RemoveGroupAdministrativeAccessRights(Int32 section, IGroup group)
        {
            CommunitySystem.CurrentContext.DefaultSecurity.RemoveGroupAdministrativeAccessRights(section, group);
        }

        public virtual void RemoveUserAdministrativeAccessRights(Int32 section, IUser user)
        {
            CommunitySystem.CurrentContext.DefaultSecurity.RemoveUserAdministrativeAccessRights(section, user);
        }

        public virtual IUser ActivateUser(Guid activationGuid)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.ActivateUser(activationGuid);
        }

        public virtual UserCollection GetLatestActivatedUsers(Int32 count)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetLatestActivatedUsers(count);
        }

        public virtual Guid AddUserToActivate(IUser user)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.AddUserToActivate(user);
        }

        public virtual IUser GetUserToActivate(Guid activationGuid)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetUserToActivate(activationGuid);
        }

        public virtual void RemoveUserToActivate(Guid activationGuid)
        {
            CommunitySystem.CurrentContext.DefaultSecurity.RemoveUserToActivate(activationGuid);
        }

        public virtual IUser GetUserToActivateByUserName(String userName)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetUserToActivateByUserName(userName);
        }

        public virtual void OnRemoveGroup(IRemoveGroupEventArgs args)
        {
            CommunitySystem.CurrentContext.DefaultSecurity.OnRemoveGroup(args);
        }

        public virtual void OnCreateGroup(ICreateGroupEventArgs args)
        {
            CommunitySystem.CurrentContext.DefaultSecurity.OnCreateGroup(args);
        }

        public virtual void OnUpdateGroup(IUpdateGroupEventArgs args)
        {
            CommunitySystem.CurrentContext.DefaultSecurity.OnUpdateGroup(args);
        }

        public virtual void OnCreateUser(ICreateUserEventArgs args)
        {
            CommunitySystem.CurrentContext.DefaultSecurity.OnCreateUser(args);
        }

        public virtual void OnRemoveUser(IRemoveUserEventArgs args)
        {
            CommunitySystem.CurrentContext.DefaultSecurity.OnRemoveUser(args);
        }

        public virtual void OnRestoreUser(IRestoreUserEventArgs args)
        {
            CommunitySystem.CurrentContext.DefaultSecurity.OnRestoreUser(args);
        }

        public virtual void OnUpdateUser(IUpdateUserEventArgs args)
        {
            CommunitySystem.CurrentContext.DefaultSecurity.OnUpdateUser(args);
        }

        public virtual void OnChangeUserPassword(IChangeUserPasswordEventArgs args)
        {
            CommunitySystem.CurrentContext.DefaultSecurity.OnChangeUserPassword(args);
        }

        public virtual Type GetAdminstrativeAccessRightType(Int32 sectionId)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetAdminstrativeAccessRightType(sectionId);
        }

        public virtual ISiteAccessRights GetTotalGroupSiteAccessRights(IGroup group, ISite site)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetTotalGroupSiteAccessRights(group, site);
        }

        public virtual IModuleAccessRights GetTotalGroupModuleAccessRights(IGroup group, String module)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetTotalGroupModuleAccessRights(group, module);
        }

        public virtual ISiteAccessRights GetGroupSiteAccessRights(IGroup group, ISite site)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetGroupSiteAccessRights(group, site);
        }

        public virtual IModuleAccessRights GetGroupModuleAccessRights(IGroup group, String module)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetGroupModuleAccessRights(group, module);
        }

        public virtual ISiteAccessRights GetTotalUserSiteAccessRights(IUser user, ISite site)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetTotalUserSiteAccessRights(user, site);
        }

        public virtual IModuleAccessRights GetTotalUserModuleAccessRights(IUser user, String module)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetTotalUserModuleAccessRights(user, module);
        }

        public virtual ISiteAccessRights GetUserSiteAccessRights(IUser user, ISite site)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetUserSiteAccessRights(user, site);
        }

        public virtual IModuleAccessRights GetUserModuleAccessRights(IUser user, String module)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetUserModuleAccessRights(user, module);
        }

        public virtual IAdministrativeAccessRights GetGroupAdministrativeAccessRights(IGroup group)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetGroupAdministrativeAccessRights(group);
        }

        public virtual IAdministrativeAccessRights GetGroupAdministrativeAccessRights(IGroup group, Type type)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetGroupAdministrativeAccessRights(group, type);
        }

        public virtual IAdministrativeAccessRights GetInheritedGroupAdministrativeAccessRights(IGroup group)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetInheritedGroupAdministrativeAccessRights(group);
        }

        public virtual IAdministrativeAccessRights GetInheritedGroupAdministrativeAccessRights(IGroup group, Type type)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetInheritedGroupAdministrativeAccessRights(group, type);
        }

        public virtual IAdministrativeAccessRights GetUserAdministrativeAccessRights(IUser user)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetUserAdministrativeAccessRights(user);
        }

        public virtual IAdministrativeAccessRights GetUserAdministrativeAccessRights(IUser user, Type type)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetUserAdministrativeAccessRights(user, type);
        }

        public virtual IAdministrativeAccessRights GetInheritedUserAdministrativeAccessRights(IUser user)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetInheritedUserAdministrativeAccessRights(user);
        }

        public virtual IAdministrativeAccessRights GetInheritedUserAdministrativeAccessRights(IUser user, Type type)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetInheritedUserAdministrativeAccessRights(user, type);
        }

        public virtual UserCollection GetQueryResult(IUserQuery query)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetQueryResult(query);
        }

        public virtual UserCollection GetQueryResult(IUserQuery query, Int32 page, Int32 pageSize)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetQueryResult(query, page, pageSize);
        }

        public virtual UserCollection GetQueryResult(IUserQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetQueryResult(query, page, pageSize, out totalItems);
        }

        public virtual GroupCollection GetQueryResult(IGroupQuery query)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetQueryResult(query);
        }

        public virtual GroupCollection GetQueryResult(IGroupQuery query, Int32 page, Int32 pageSize)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetQueryResult(query, page, pageSize);
        }

        public virtual GroupCollection GetQueryResult(IGroupQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return CommunitySystem.CurrentContext.DefaultSecurity.GetQueryResult(query, page, pageSize, out totalItems);
        }

        #endregion

        public virtual void OnRemovedGroup(ISecurityHandler sender, IRemoveGroupEventArgs args)
        {
            if (RemovedGroup != null)
                RemovedGroup(sender, args);
        }

        public virtual void OnCreatedGroup(ISecurityHandler sender, ICreateGroupEventArgs args)
        {
            if (CreatedGroup != null)
                CreatedGroup(sender, args);
        }

        public virtual void OnUpdatedGroup(ISecurityHandler sender, IUpdateGroupEventArgs args)
        {
            if (UpdatedGroup != null)
                UpdatedGroup(sender, args);
        }

        public virtual void OnCreatedUser(ISecurityHandler sender, ICreateUserEventArgs args)
        {
            if (CreatedUser != null)
                CreatedUser(sender, args);
        }

        public virtual void OnRemovedUser(ISecurityHandler sender, IRemoveUserEventArgs args)
        {
            if (RemovedUser != null)
                RemovedUser(sender, args);
        }

        public virtual void OnRestoredUser(ISecurityHandler sender, IRestoreUserEventArgs args)
        {
            if (RestoredUser != null)
                RestoredUser(sender, args);
        }

        public virtual void OnUpdatedUser(ISecurityHandler sender, IUpdateUserEventArgs args)
        {
            if (UpdatedUser != null)
                UpdatedUser(sender, args);
        }

        public virtual void OnChangedUserPassword(ISecurityHandler sender, IChangeUserPasswordEventArgs args)
        {
            if (ChangedUserPassword != null)
                ChangedUserPassword(sender, args);
        }
    }
}